library Wave11a initializer init requires LegacySystem

	globals
		private location udg_AirstrikeTarget
	endglobals
	
    private function F22SurgicalStrike takes nothing returns nothing
        local integer i = 1
        local group missiles
        local unit u
        local real x = GetUnitX(udg_Lyganesh)
        local real y = GetUnitY(udg_Lyganesh)
        local real dx = GetUnitX(udg_F22Raptor[1]) - x
        local real dy = GetUnitY(udg_F22Raptor[1]) - y
        local real dx2 = GetUnitX(udg_F22Raptor[2]) - x
        local real dy2 = GetUnitY(udg_F22Raptor[2]) - y
        local real angle = bj_RADTODEG * Atan2(y - GetUnitY(udg_F22Raptor[1]), x - GetUnitX(udg_F22Raptor[1]))
        local real x2 = x + 800.0*Cos(angle*bj_DEGTORAD)
        local real y2 = y + 800.0*Sin(angle*bj_DEGTORAD)
        call IssuePointOrder(udg_F22Raptor[1],"move",x2,y2)
        call IssuePointOrder(udg_F22Raptor[2],"move",x2+60.0,y2+60.0)
        call IssuePointOrder(udg_F22Raptor[3],"move",x2-60.0,y2-60.0)
        if GetWidgetLife(udg_Lyganesh)<2500.0 then
            call DisableTrigger(GetTriggeringTrigger())
            call GeneralText(8.0, "|cfff3ad00F-22 Pilot|r : Target neutralized. Aborting mission." )
            call SetUnitFlyHeight(udg_F22Raptor[1], 1200.0, 200.0)
            call SetUnitFlyHeight(udg_F22Raptor[2], 1200.0, 200.0)
            call SetUnitFlyHeight(udg_F22Raptor[3], 1200.0, 200.0)
            call PolledWait(2.)
            call RemoveUnit(udg_F22Raptor[1])
            call RemoveUnit(udg_F22Raptor[2])
            call RemoveUnit(udg_F22Raptor[3])
            call SetForceAllianceStateBJ( bj_FORCE_PLAYER[9], udg_EchoCompany, bj_ALLIANCE_ALLIED )
            call SetForceAllianceStateBJ( bj_FORCE_PLAYER[9], udg_UnknownEntities, bj_ALLIANCE_UNALLIED )
        elseif (dx*dx+dy*dy<=4000000.0) or (dx2*dx2+dy2*dy2<=4000000.0) then //2000 radius
            call DisableTrigger(GetTriggeringTrigger())
            set udg_AirstrikeTarget = GetUnitLoc(udg_Lyganesh)
            call SetForceAllianceStateBJ( bj_FORCE_PLAYER[9], udg_UnknownEntities, bj_ALLIANCE_ALLIED )
            call GeneralText(8.0, "|cfff3ad00F-22 Pilot|r : Missiles fired. He's all yours." )
            set udg_soundpath = "Abilities\\Spells\\Other\\TinkerRocket\\TinkerMissileLaunch1.wav"
            call PlaySoundPath.execute()
            set missiles = CreateGroup()
            loop
                exitwhen i>3
                set u = CreateUnit( Player(9), 'h00C', GetUnitX(udg_F22Raptor[i])+8.0, GetUnitY(udg_F22Raptor[i])+8.0 ,0 )
                call IssuePointOrder(u,"attackground",x,y)
                call GroupAddUnit(missiles,u)
                set u = CreateUnit( Player(9), 'h00C', GetUnitX(udg_F22Raptor[i])-8.0, GetUnitY(udg_F22Raptor[i])-8.0 ,0 )
                call IssuePointOrder(u,"attackground",x,y)
                call GroupAddUnit(missiles,u)
                if GetRandomInt(0,1)==1 then
                    call TriggerSleepAction(0.)
                endif
                set i=i+1
            endloop
            set u = null
            set udg_soundpath = "Abilities\\Spells\\Other\\TinkerRocket\\TinkerMissileLaunch1.wav"
            call PlaySoundPath.execute()
            call ArtHandler(udg_F22Raptor[1],GetLocationX(udg_AirstrikeTarget), GetLocationY(udg_AirstrikeTarget),15,500.0,500.0,0.3,2)
            call SetUnitFlyHeight(udg_F22Raptor[1],1200.0,200.0)
            call SetUnitFlyHeight(udg_F22Raptor[2],1200.0,200.0)
            call SetUnitFlyHeight(udg_F22Raptor[3],1200.0,200.0)
            call SetUnitMoveSpeed(udg_Lyganesh,GetUnitDefaultMoveSpeed(udg_Lyganesh))
            call PolledWait(1.)
            call RemoveLocation(udg_AirstrikeTarget)
            call ForGroup(missiles,function RemoveUnitAll)
            call DestroyGroup(missiles)
            set missiles = null
            set udg_soundpath = "Sound\\Interface\\GlueScreenMeteorLaunch3.wav"
            call PlaySoundPath.execute()
            call SetForceAllianceStateBJ(bj_FORCE_PLAYER[9],udg_EchoCompany,bj_ALLIANCE_ALLIED)
            call SetForceAllianceStateBJ(bj_FORCE_PLAYER[9],udg_UnknownEntities,bj_ALLIANCE_UNALLIED)
            call PolledWait(0.5)
            call RemoveUnit(udg_F22Raptor[1])
            call RemoveUnit(udg_F22Raptor[2])
            call RemoveUnit(udg_F22Raptor[3])
        endif
		
		set u = null
		set missiles = null
    endfunction

    private function F22LygaMissionConditions takes nothing returns boolean
        local unit u = GetTriggerUnit()
        local real hp = GetWidgetLife(u)
        return (GetUnitTypeId(u)=='N006' and hp<=32000.0) or (GetUnitTypeId(u)=='N00F' and hp<=38000.0)
    endfunction

    private function F22LygaMission takes nothing returns nothing
        local integer i = 1
        local trigger t = CreateTrigger()
        call DisableTrigger(GetTriggeringTrigger())
        call PolledWait(2.)
        call GeneralText(5.0, "|cfff3ad00Battalion|r : Birds are inbound. Hang on, Marines." )
        loop
            exitwhen i>3
            set udg_F22Raptor[i] = CreateUnit(Player(9),'h00B',-8300.0+(75.0*I2R(i)),5750.0+(75.0*I2R(i)),0.0)
            call TriggerSleepAction(0.)
            set i=i+1
        endloop
        call PolledWait(2.)
        call TriggerAddAction(t, function F22SurgicalStrike)
        call TriggerRegisterTimerEvent(t,0.2,true)
        set t = null
        call PolledWait(4.)
        call SetUnitMoveSpeed(udg_Lyganesh,GetUnitDefaultMoveSpeed(udg_Lyganesh)*0.6)
        call SetForceAllianceStateBJ( bj_FORCE_PLAYER[9], udg_EchoCompany, bj_ALLIANCE_ALLIED_VISION )
        set i = 1
        loop
            exitwhen i>udg_InitialPlayers
            call UnitShareVision(udg_F22Raptor[1],ConvertedPlayer(udg_Sorted[i]),true)
            call UnitShareVision(udg_F22Raptor[2],ConvertedPlayer(udg_Sorted[i]),true)
            call UnitShareVision(udg_F22Raptor[3],ConvertedPlayer(udg_Sorted[i]),true)
            set i=i+1
        endloop
        call StartSound(gg_snd_F22Pilot)
    endfunction
    
    private function Wave11a takes nothing returns nothing
        local location l = GetRandomLocInRect(udg_UndeadRandomSpawn[GetRandomInt(1,4)])
        local integer i
        local trigger t
        
        if udg_Players<=4 then
            set i = 'N006' //Lyganesh Variant A
        else
            set i = 'N00F'//Lyganesh Variant B
        endif
        set udg_Lyganesh = CreateUnit(Player(14),i,GetLocationX(l),GetLocationY(l),0.0)
        call RemoveLocation(l)
        set l = null
        
        call SuspendHeroXP(udg_Lyganesh,true)
        if udg_SquadXP < 40000 or ( udg_SquadXP > 40000 and GetRandomInt(0, 1) == 1 ) then
            set t = CreateTrigger()
            call TriggerAddCondition( t, Condition( function F22LygaMissionConditions ) )
            call TriggerAddAction( t, function F22LygaMission )
            call TriggerRegisterUnitEvent(t, udg_Lyganesh, EVENT_UNIT_ATTACKED)
        endif

        set udg_CurrentWave = 11
        call GenericActions.execute()
        call RandomSpawn('n002',1,udg_Players) //Devourer
        call SpawnRectRandom(gg_rct_SpawnEastCave,Player(14),'n004',2+udg_Spawn)
        call SpawnRectRandom(gg_rct_SpawnEastBottom,Player(13),'n003',udg_Spawn)
        call SpawnRectRandom(gg_rct_SpawnNorthWaterfall,Player(14),'n004',2+udg_Spawn)
        call SpawnRectRandom(gg_rct_SpawnSouthWaterfall,Player(13),'n003',udg_Spawn)
        call GeneralText(8.0, "|cffcd950cBattalion|r : Zack's pouring in your sector. You'll have to sweep the sector for supplies or you'll be overwhelmed." )
        call PolledWait(4.)
        call GeneralText(8.0, "|cffcd950cMarine|r : We're doing our best." )
        call SpawnRectRandom(gg_rct_SpawnWestTop,Player(14),'n004',2+udg_Spawn)
        call SpawnRectRandom(gg_rct_SpawnSouthWaterfall,Player(14),'n004',2+udg_Spawn)
        call SpawnRectRandom(gg_rct_SpawnWestTop,Player(13),'n00C',1)
        call GeneralText(8.0, "|cffcd950cBattalion|r : We know. Watch your backs." )
        call PolledWait(5.)
        call StartSound(gg_snd_FattyHorde)
        call PolledWait(10.0*udg_Pace)
        call GankMarine(Player(13),'n004',2+udg_Spawn,1600,2000)
        call GankMarine(Player(13),'n004',2+udg_Spawn,1600,2000)
        call PolledWait(30.0*udg_Pace)
        call LightningAmbience.execute()
        call GankMarine(Player(14),'n004',2+udg_Spawn,1600,2000)
        call GankMarine(Player(13),'n004',2+udg_Spawn,1600,2000)
        call PolledWait(30.0*udg_Pace)
        if GetRandomInt(1,4)==1 then
            call GeneralText(2.5, "|cffcd950cMarine|r : When are we getting close air support?" )
        endif
        call GankMarine(Player(13),'n004',2 + udg_Spawn,1600,2000)
        call SpawnRectRandom(gg_rct_SpawnRiverCave,Player(14),'n00C',1)
        call PolledWait(30.0*udg_Pace)
        if udg_Players==udg_InitialPlayers then
            call GankMarine(Player(14),'u005',2+udg_Spawn,1600,2000)
        endif
        call GankMarine(Player(13),'n004',3+udg_Spawn,1600,2000)
        call PolledWait(30.0*udg_Pace)
        if udg_HitsTaken<120 then
            call GankMarine(Player(13),'u004',6+udg_Spawn,1600,2000)
        endif
        call LightningAmbience.execute()
        call RandomSpawn('u005',1,GetRandomInt(1,udg_Players)) //Gargolye #2
        call GankMarine(Player(14),'n004',2+udg_Spawn,1600,2000)
        call GankMarine(Player(13),'n004',2+udg_Spawn,1600,2000)
        call SetRevelationSpawnEnabled(false)
        call PolledWait(30.0*udg_Pace)
        if udg_Players>0 then
            call ExecuteRegisteredFunction("Wave12a")
            //call Wave12a.execute()
        endif
    endfunction
    
    private function init takes nothing returns nothing
        call RegisterFunction("Wave11a", function Wave11a)
        // call ExecuteRegisteredFunction("Wave11a")
    endfunction
    
endlibrary

